import java.util.HashSet;
import java.util.Set;


public class Test {
    //写法一半暴力，结合字符串用法
    public int strStr(String haystack, String needle) {
        int left=0,right=needle.length()-1;
        while(right<=haystack.length()-1){
            if(haystack.substring(left,right+1).equals(needle)){
                return left;
            }
            left++;
            right++;
        }
        return -1;
    }

    //写法二，双指针
    public int strStr1(String haystack,String needle){
        int n=haystack.length(),m=needle.length();
        //处理特殊情况
        if(m==0)return 0;
        if(n<m)return -1;
        for (int left = 0,right=0; left < n-m; left++) {
            while(right<m&&haystack.charAt(left+right)==needle.charAt(right)){
                right++;
            }
            if(right==m)return left;
        }
        return -1;
    }

    public static void main(String[] args) {
        String haystack="abcfefefif",needle="fef";
        Test test=new Test();
        System.out.println("方法一:"+test.strStr(haystack, needle));
        System.out.println("方法二:"+test.strStr1(haystack, needle));
    }
}
